﻿#include "EliteReplacement.h"
#include "EvolutionaryAlgorithm.h"
#include "Configuration.h"
#include "Printer.h"

#include <algorithm>

EvolutionaryAlgorithm::population_type & EliteReplacement::replace(EvolutionaryAlgorithm::population_type & actualPopulation, const EvolutionaryAlgorithm::population_type & children)
{
	//EvolutionaryAlgorithm::population_type choosenPopulation;
	//// sety sa juz posortowane
	//EvolutionaryAlgorithm::population_type::iterator iter1 = actualPopulation.begin();
	//EvolutionaryAlgorithm::population_type::iterator iter2 = children.begin();
	//while (choosenPopulation.size() < Configuration::u && iter1 != actualPopulation.end() && iter2 != children.end())
	//{
	//	double fit1 = iter1->fitnessValue;
	//	double fit2 = iter2->fitnessValue;
	//	//std::cout << "elite fit1 fit2 " << fit1 << " " << fit2 <<std::endl;
	//	//std::cout << *iter1 << *iter2;
	//	if (fit1 > fit2)
	//	{
	//		choosenPopulation.insert(*iter1);
	//		++iter1;
	//	}
	//	else
	//	{
	//		choosenPopulation.insert(*iter2);
	//		++iter2;
	//	}
	//}
	//while (choosenPopulation.size() < Configuration::u) // tutaj children skonczyly sie
	//{
	//	choosenPopulation.insert(*iter1);
	//	++iter1;
	//}
	////std::cout<<"ost pop"<<std::endl<<std::endl;
	/////Printer::ciout(actualPopulation);
	////Printer::ciout(children);
	////Printer::ciout(choosenPopulation);
	////std::cout<<"koniec metody"<<std::endl<<std::endl;
	//return choosenPopulation;

	//Printer::ciout(actualPopulation);
	//std::cout << "przed " << actualPopulation.size();
	unsigned int sizeBefore = actualPopulation.size();
	actualPopulation.insert(children.begin(), children.end());
	EvolutionaryAlgorithm::population_type::iterator iter1 = actualPopulation.begin();
	std::advance(iter1, sizeBefore);
	actualPopulation.erase(iter1, actualPopulation.end());
	//Printer::ciout(actualPopulation);
	//std::cout << "po " << actualPopulation.size();
	return actualPopulation;
}
